<?php

namespace App\Http\Controllers;

use App\Models\Classify;
use Illuminate\Http\Request;
use App\Service\DataService;
use App\Models\Log;

class ClassifyController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('classify.list', ['classify'=>Classify::toTree()]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $model = new Classify();
        $classify = DataService::handleDate($model,$request->all(),'classify-add_or_update');
        if($classify['status']==1)Log::addLogs(trans('xxm.classify.handle_classify').trans('fzs.common.success'),'/classify/story');
        else Log::addLogs(trans('xxm.classify.handle_classify').trans('fzs.common.fail'),'/classify/destroy');
        return $classify;
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Classify  $classify
     * @return \Illuminate\Http\Response
     */
    public function show(Classify $classify)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Classify  $classify
     * @return \Illuminate\Http\Response
     */
    public function edit($id=0)
    {
        $classify = ($id > 0) ? Classify::findByRoleId($id) : [];
        return view('classify.edit', ['id'=>$id,'classify'=>$classify,'classifys'=>Classify::toTree()]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Classify  $classify
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Classify $classify)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Classify  $classify
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //验证分类下是否有商品 有商品不给删除
//        if (is_config_id($id, "admin.menu_table_cannot_manage_ids", false))return $this->resultJson('fzs.menus.notdel', 0);
        $model = new Classify();
        $classify= DataService::handleDate($model,['id'=>$id],'classify-delete');
        if($classify['status']==1)Log::addLogs(trans('xxm.classify.del_classify').trans('fzs.common.success'),'/classify/destroy/'.$id);
        else Log::addLogs(trans('xxm.classify.del_classify').trans('fzs.common.fail'),'/menus/destroy/'.$id);
        return $classify;
    }

}
